package com.yuntu.service;

import com.yuntu.dao.UserMapper;
import com.yuntu.pojo.User;
import com.yuntu.tools.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    /**
     * 登录
     */
    @Override
    public User login(String devCode, String devPassword) throws Exception {
        User user = userMapper.getLogin(devCode);
        if (user != null){
            if (!user.getDevPassword().equals(devPassword)){
                user = null;
            }
        }
        return user;
    }

    /**
     * 分页
     */
    @Override
    public List<User> getPageList(PageUtil<User> pageUtil) throws Exception {
        List<User> userList = new ArrayList<>();
        int count = userMapper.getCount();
        System.out.println("-------------"+count);
        pageUtil.setCounts(count);
        if (count > 0){
            if (pageUtil.getPageindex() > pageUtil.getPagecount()){
                pageUtil.setPageindex(pageUtil.getPagecount());
            }
            userList = userMapper.getPageAll(pageUtil);
            pageUtil.setLists(userList);
        }
        return userList;
    }

    /**
     * 添加
     */
    @Override
    public boolean add(User user) throws Exception {
        boolean flag = false;
        if (userMapper.addUser(user) > 0){
            flag = true;
        }
        return flag;
    }

    /**
     * 删除
     */
    @Override
    public boolean del(int id) throws Exception {
        boolean flag = false;
        if (userMapper.delUser(id) > 0){
            flag = true;
        }
        return flag;
    }

    /**
     * 根据id查询信息
     */
    @Override
    public User getUserById(int id) throws Exception {
        User user = userMapper.getById(id);
        return user;
    }

    /**
     * 修改
     */
    @Override
    public boolean update(User user) throws Exception {
        boolean flag = false;
        if (userMapper.updateUser(user) > 0){
            flag = true;
        }
        return flag;
    }

    /**
     * 验证重名
     */
    @Override
    public User exist(String devCode) throws Exception {
        User user = userMapper.getExits(devCode);
        return user;
    }
}
